package com.yunhe.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.yunhe.bean.OrderInfo;
import com.yunhe.inter.OrderInfoInter;
import com.yunhe.util.SqlSessionFactoryUtil;

/**
 * 处理直接点击购买的方法生成订单不经过购物车
 */
@WebServlet("/ShoppingResult_1")
public class ShoppingResult_1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
    public ShoppingResult_1() {
        super();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("进入servlet......");
		Integer commodityID = Integer.parseInt(request.getParameter("commodityID"));
		String address = request.getParameter("address");
		String addressString = new String(address.getBytes("ISO-8859-1"),"UTF-8");
		
		//创建订单对象
		OrderInfo oi = new OrderInfo();
		//把对应的商品id添加到对象
		oi.setCommodityID(commodityID);
		//把地址添加到对象
		oi.setDetailedAddress(addressString);
		//购买数量添加到对象
		oi.setQuantityPurchased(oi.getQuantityPurchased());
		oi.setUserID(1);
		Date currentTime = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String dateString = sdf.format(currentTime);
		//当前时间戳加上用户ID，暂时用ID=1，合并后从session中获取
		String orderSerialNumber =dateString+"1";
		oi.setOrderSerialNumber(orderSerialNumber);
		//获取数据库连接
		SqlSession sqlSession =SqlSessionFactoryUtil.openSqlSession();
		OrderInfoInter oiMapper = sqlSession.getMapper(OrderInfoInter.class);
		//调用存储过程，生成对应订单
		oiMapper.addOrderInfoByPro(oi);
		
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
